package org.shop.controller.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.ShopClassify;
import org.shop.service.ShopClassifyService;
import org.shop.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ShopController {
	private static Logger logger = LogManager.getLogger(ShopController.class);
	@Autowired
    private ShopService shopService;
	@Autowired
	private ShopClassifyService shopClassifyService;
	/**
	 * 获取所有店铺
	 * @param req
	 * @return
	 */
	@RequestMapping("app/getShopList")
	public String getShopList(HttpServletRequest req){
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String shopName = req.getParameter("shopName");
		String classifyId = req.getParameter("classifyId");
		String merchantType = req.getParameter("merchantType");
		//获取登录人用户信息
//		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {
        	Map<String, Object> params = new HashMap<String, Object>();
        	params.put("merchantName", shopName);
        	params.put("schoolId", (long)1);
        	params.put("classifyId", classifyId);
        	params.put("merchantType", merchantType);
        	list = shopService.getShopList(params);
        	req.setAttribute("shopList", list);
        	List<ShopClassify> classifyList = shopClassifyService.getShopClassifyList();
        	req.setAttribute("classifyList", classifyList);
        	
        } catch(Exception e) {
            e.printStackTrace();
            logger.error(e);
        }
		return "app/shopList";
	}
	
	/**
	 * 获取所有学生店铺
	 * @param req
	 * @return
	 */
	@RequestMapping("app/getStudentShopList")
	public String getStudentShopList(HttpServletRequest req){
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String shopName = req.getParameter("shopName");
		String classifyId = req.getParameter("classifyId");
		String merchantType = req.getParameter("merchantType");
		//获取登录人用户信息
//		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try {
        	Map<String, Object> params = new HashMap<String, Object>();
        	params.put("merchantName", shopName);
        	params.put("schoolId", (long)1);
        	params.put("classifyId", classifyId);
        	params.put("merchantType", merchantType);
        	list = shopService.getShopList(params);
        	req.setAttribute("shopList", list);
        	List<ShopClassify> classifyList = shopClassifyService.getShopClassifyList();
        	req.setAttribute("classifyList", classifyList);
        	
        } catch(Exception e) {
            e.printStackTrace();
            logger.error(e);
        }
		return "app/studentShopList";
	}
	
	/**
	 * 根据类型获取商家
	 * @param req
	 * @return
	 */
	@RequestMapping("app/getShopListByClassifyId")
	@ResponseBody
	public List<Map<String, Object>> getShopListByClassifyId(HttpServletRequest req){
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String classifyId = req.getParameter("classifyId");
		String merchantType = req.getParameter("merchantType");
        try {
        	Map<String, Object> params = new HashMap<String, Object>();
        	params.put("classifyId", classifyId);
        	params.put("merchantType", merchantType);
        	list = shopService.getShopList(params);
        	req.setAttribute("shopList", list);
        } catch(Exception e) {
            e.printStackTrace();
            logger.error(e);
        }
		return list;
	}
	
}
